package i003.me.letterstudy;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.Locale;
import java.util.Random;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity
{
    private static final String KEY_FIRST_LETTER = "FirstLetter";

    private static final String KEY_SECOND_LETTER = "SecondLetter";

    @BindView(R.id.imgLetter)
    ImageView imgLetter;

    @BindView(R.id.txtUpperCase)
    TextView txtUpperCase;

    @BindView(R.id.btnSpeak)
    TextView btnSpeak;

    private char firstLetter, secondLetter, lastLetter;

    private Random random;

    private TextToSpeech textToSpeech;

    public static void start(Activity activity, char firstLetter, char secondLetter)
    {
        Intent intent = new Intent(activity, MainActivity.class);
        intent.putExtra(KEY_FIRST_LETTER, Character.toLowerCase(firstLetter));
        intent.putExtra(KEY_SECOND_LETTER, Character.toLowerCase(secondLetter));
        activity.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        this.random = new Random();
        this.textToSpeech = new TextToSpeech(this, new OnInitListener()
        {
            @Override
            public void onInit(int status)
            {
                if(status == TextToSpeech.SUCCESS)
                {
                    int result = textToSpeech.setLanguage(Locale.ENGLISH);

                    if(result != TextToSpeech.LANG_MISSING_DATA && result != TextToSpeech.LANG_NOT_SUPPORTED)
                    {
                        textToSpeech.setPitch(10f);
                        btnSpeak.setVisibility(View.VISIBLE);
                    }
                }
            }
        });
        Intent intent = this.getIntent();
        this.firstLetter = intent.getCharExtra(KEY_FIRST_LETTER, 'a');
        this.secondLetter = intent.getCharExtra(KEY_SECOND_LETTER, 'z');
        switchLetter();
    }

    @Override
    protected void onDestroy()
    {
        textToSpeech.stop();
        textToSpeech.shutdown();
        super.onDestroy();
    }

    @OnClick({R.id.rootView, R.id.btnUpperCase, R.id.btnSpeak})
    public void onClick(View view)
    {
        switch(view.getId())
        {
            case R.id.rootView:
            {
                switchLetter();
                break;
            }
            case R.id.btnUpperCase:
            {
                String text = imgLetter.getTag().toString().toUpperCase();
                txtUpperCase.setText(text + " " + text.toLowerCase());
                break;
            }
            case R.id.btnSpeak:
            {
                speakText(imgLetter.getTag().toString().toUpperCase());
                break;
            }
        }
    }

    private void switchLetter()
    {
        int delta = secondLetter - firstLetter + 1;
        char newLetter = (char)(firstLetter + random.nextInt(delta));

        if(newLetter != lastLetter)
        {
            this.lastLetter = newLetter;
            String letter = String.valueOf(newLetter);
            imgLetter.setTag(letter);
            imgLetter.setImageResource(getResources().getIdentifier(letter, "drawable", getPackageName()));
            txtUpperCase.setText(null);
        }
        else
        {
            switchLetter();
        }
    }

    private void speakText(String text)
    {
        textToSpeech.stop();
        textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, null, null);
    }
}